package com.tiansheng.microservice.usercenter.config;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
 * 
     * 此类描述的是:
     * @author: zhanghr
     * @version: 1.0 
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {

	 @Value("${swagger.show}")
	    private boolean swaggerShow;
    private String string;
    private String header;

    @Bean
    public Docket wuyxApi() {


        List<Parameter> pars = new ArrayList<>();

        ParameterBuilder tokenPar = new ParameterBuilder();
        string = "string";
        header = "header";
        tokenPar.name("X-JWT").description("令牌").modelRef(new ModelRef(string)).parameterType(header).required(false).build();
        ParameterBuilder userId = new ParameterBuilder();
        userId.name("userCode").description("用户编码").modelRef(new ModelRef(string)).parameterType(header).required(false).build();

        ParameterBuilder companyCode = new ParameterBuilder();
        companyCode.name("companyCode").description("公司code").modelRef(new ModelRef(string)).parameterType(header).required(false).build();

        pars.add(tokenPar.build());
        pars.add(userId.build());
        pars.add(companyCode.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .enable(swaggerShow)
                .groupName("api-TradeCenter")
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(UserCenterModel.class))
                .paths(PathSelectors.any())
                .build()
                .globalOperationParameters(pars)
                .apiInfo(wuyxInfo());
    }


    private ApiInfo wuyxInfo() {
        return new ApiInfoBuilder().title("用户中心系统文档")
                .description("更多请关注：XXX:8877/doc.html")
                .version("0.1")
                .termsOfServiceUrl("usercenter").build();
    }
   

    /****************************************************************************************************************/


    /**
     * SpringBoot默认已经将classpath:/META-INF/resources/和classpath:/META-INF/resources/webjars/映射
     * 所以该方法不需要重写，如果在SpringMVC中，可能需要重写定义（我没有尝试）
     * 重写该方法需要 extends WebMvcConfigurerAdapter
     *
     */

     public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
        
        registry.addResourceHandler("/Tleaf/**")
        .addResourceLocations("classpath:/templates/");
    }

}
